package com.example.myapplication.Config;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.ShuJu.ViewlistBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpTool {

    // public static String TOKEN =  TokenBean.token;
    public static String TOKEN;
    public static String URL = "http://124.93.196.45:10001/";

    public static final String GET = "get";
    public static final String TAG = "TAG";
    public static final String POST = "post";
    public static final String PUT = "put";

    private static OkHttpClient okHttpClient;
    private static Gson gson;

    public static void init() {
        okHttpClient = null;
        gson = null;
        okHttpClient = new OkHttpClient();
        gson = new Gson();
    }

    public static void doNetRequest(String url, String method, Class classOfT, NetCallback ncb, String... jsonParames) {
        Request.Builder rb = new Request.Builder();
        if (!TextUtils.isEmpty(url)) {
            url = URL + url;
            Log.v("OKHTTPTool", url);
            rb.url(url);
        }
        if (!TextUtils.isEmpty(TOKEN)) {
            rb.addHeader("Authorization", TOKEN);
        }
        if (GET.equals(method)) {
            rb.get();
        } else if (POST.equals(method) && jsonParames.length > 0) {
            rb.post(RequestBody.create(jsonParames[0], MediaType.parse("application/json; charset=utf-8")));
        } else if (PUT.equals(method) && jsonParames.length > 0) {
            rb.put(RequestBody.create(jsonParames[0], MediaType.parse("application/json; charset=utf-8")));
        }
        if (okHttpClient == null || gson == null) {
            init();
        }
        Request request = rb.build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("OKHTTPTool", e.getMessage());
                ncb.onFailure("Request failure.");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws
                    IOException {
                String json = response.body().string();
                Log.d("OKHTTPTool", "Result:" + json);
                try {
                    Object obj = gson.fromJson(json, classOfT);
                    ncb.onSuccess(obj);
                } catch (Exception e) {
                    Log.d("OKHTTPTool", "Exception:" + e.getMessage());
                    ncb.onFailure("Request failure.Json parse exception");
                }
            }
        });
    }

    public interface NetCallback {
        void onSuccess(Object obj);

        void onFailure(String msg);
    }

}